﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using Utils = PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
//using UserContext = PHSRAG.Insight21.Context.UserContext;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

#endregion

namespace PHSRAG.Insight21.Agreements.Reports {

    
    public class sqlRevenueBreakdownReport : sqlReport
    {
        public const string DictionaryFundNumberKey = "DictionaryFundNumberKey_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";
        public const string DictionaryJournalStartDateKey = "DictionaryJournalStartDateKey_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";
        public const string DictionaryJournalEndDateKey = "DictionaryJournalEndDateKey_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";
        public const string DictionaryRevenueTotalKey = "DictionaryRevenueTotalKey_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";
        public const string SortColumnKey = "SortColumn_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";
        public const string SortDirectionKey = "SortAscending_8021F7C5-E6F6-4fac-AB4C-4291DFDA1E3B";        
        private const string AsofDateKey = "glPostAsOfDate";
    
        public sqlRevenueBreakdownReport(string pageUrl):base(pageUrl)
        {
            doCustomExcelReport = true;                  
        }
        
        public override string ReportName {
            get {
                return "Agmt_RevenueBreakdownReport";
            }
           
        }

        public override ParameterValue[] setParameters(ref string rptName)
        {
            reportTitle = "Revenue Breakdown";
            rptName = rptFormat=="Excel"?ReportName+"Excel":ReportName;

            reportSource = GetRevenueBreakdownResultSet(ResultSetType.Full).GetDataContainer();

            reportCriteria = string.Format("{0} As of {1}",              //Start Date: {1}; End Date: {2}",
                DictionaryFundNumber==""?"":"Fund #: " + DictionaryFundNumber,
                //DictionaryJournalStartDate,
                DictionaryJournalEndDate);

            return base.setParameters(ref rptName);
        }

        private string DictionaryJournalStartDate
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return "";
                object dictionaryJournalStartDate = revenueBreakdownPageCache[DictionaryJournalStartDateKey];
                return (dictionaryJournalStartDate == null) ? "" : (string)dictionaryJournalStartDate;
            }
        }

        private string DictionaryJournalEndDate
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return "";
                object dictionaryJournalEndDate = revenueBreakdownPageCache[DictionaryJournalEndDateKey];
                return (dictionaryJournalEndDate == null) ? "" : (string)dictionaryJournalEndDate;
            }
        }

        private string DictionaryRevenueTotal
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return "";
                object dictionaryRevenueTotal = revenueBreakdownPageCache[DictionaryRevenueTotalKey];
                return (dictionaryRevenueTotal == null) ? "" : (string)dictionaryRevenueTotal;
            }
        }

        private Agreements.RevenueBreakdowns GetRevenueBreakdownResultSet(ResultSetType resultType)
        {
            Agreements.RevenueBreakdowns revenueBreakdowns = new Agreements.RevenueBreakdowns();
            //UserContext userContext = (UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey);
            System.Diagnostics.Debug.Assert(userContext != null);
            bool searchByFundNumber = DictionaryFundNumber != string.Empty;

            // If no total data is available in the cached dictionary entries, then populate it through load along with the IDs.
            //revenueBreakdowns.Load(Utility.DataAccess.GetConnectionString(), Session.SessionID,
            //    (searchByFundNumber)
            //        ? (object)userContext.DomainUserId
            //    : searchCriteria,
            //    (searchByFundNumber) ? DictionaryFundNumber : string.Empty,
            //    searchCriteria[Constants.RevenueFromDateKey],
            //    searchCriteria[Constants.RevenueToDateKey],
            //    ((DictionaryRevenueTotal == "") ? false : true),
            //    SortColumn,
            //    (SortAscending) ? "asc" : "desc",
            //    resultType,
            //    DictionaryCumulative,
            //    searchCriteria[Constants.RevenueAccountNumber]);
            //return revenueBreakdowns;

            //Tkt:-9219 End

            Hashtable accountno = (Hashtable)cachePolicy.GetCachedObject(AgreementsConstants.AgreementDetailsKey);
            Hashtable dictionary = pageCachePolicy.GetCachedObject(Keys.AccountNumberKey) as Hashtable;
            if (accountno == null)
            {

                if (dictionary == null)
                    throw new Exception("Fund Number for which Transaction to be displayed is not provided");

                string Accountnumber = dictionary[Keys.AccountNumberKey] as string;
                revenueBreakdowns.Load(Utility.DataAccess.GetConnectionString(), Session.SessionID,
                    (searchByFundNumber)
                        ? (object)userContext.DomainUserId
                    : (object)searchCriteria,
                    (searchByFundNumber) ? DictionaryFundNumber : string.Empty,
                    searchCriteria[InsightUtilities.Constants.RevenueFromDateKey],
                    searchCriteria[InsightUtilities.Constants.RevenueToDateKey],
                    ((DictionaryRevenueTotal == "") ? false : true),
                    SortColumn,
                    (SortAscending) ? "asc" : "desc",
                    resultType,
                    DictionaryCumulative,
                    Accountnumber != "&nbsp;" ? Accountnumber : searchCriteria[InsightUtilities.Constants.RevenueAccountNumber]);

            }
            else
            {
                string Accountnumber = accountno[Keys.AccountNumberKey] as string;
                revenueBreakdowns.Load(Utility.DataAccess.GetConnectionString(), Session.SessionID,
                    (searchByFundNumber)
                        ? (object)userContext.DomainUserId
                    : (object)searchCriteria,
                    (searchByFundNumber) ? DictionaryFundNumber : string.Empty,
                    searchCriteria[InsightUtilities.Constants.RevenueFromDateKey],
                    searchCriteria[InsightUtilities.Constants.RevenueToDateKey],
                    ((DictionaryRevenueTotal == "") ? false : true),
                    SortColumn,
                    (SortAscending) ? "asc" : "desc",
                    resultType,
                    DictionaryCumulative,
                    Accountnumber != "&nbsp;" ? Accountnumber : searchCriteria[InsightUtilities.Constants.RevenueAccountNumber]);
            }

            //Tkt:-9219 End
            return revenueBreakdowns;

            
        }

        private string DictionaryFundNumber
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return "";
                object dictionaryFundNumber = revenueBreakdownPageCache[DictionaryFundNumberKey];
                return (dictionaryFundNumber == null) ? "" : (string)dictionaryFundNumber;
            }
        }

        private string SortColumn
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return "FundNumber";
                object sortColumn = revenueBreakdownPageCache[SortColumnKey];
                return (sortColumn == null) ? "FundNumber" : (string)sortColumn;
            }

            set
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) revenueBreakdownPageCache = new Hashtable();
                revenueBreakdownPageCache[SortColumnKey] = value;
                pageCachePolicy.Cache(Keys.RevenueBreakdownPageKey, revenueBreakdownPageCache);
            }
        }

        private bool SortAscending
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return true;
                object sortAscending = revenueBreakdownPageCache[SortDirectionKey];
                return (sortAscending == null) ? true : (bool)sortAscending;
            }

            set
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) revenueBreakdownPageCache = new Hashtable();
                revenueBreakdownPageCache[SortDirectionKey] = value;
                pageCachePolicy.Cache(Keys.RevenueBreakdownPageKey, revenueBreakdownPageCache);
            }
        }

        private int DictionaryCumulative
        {
            get
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null) return 0;
                object dictionaryCumulative = revenueBreakdownPageCache[Keys.CumulativeKey];
                return (dictionaryCumulative == null) ? 0 : (int)dictionaryCumulative;
            }
            set
            {
                Hashtable revenueBreakdownPageCache = pageCachePolicy.GetCachedObject(Keys.RevenueBreakdownPageKey) as Hashtable;
                if (revenueBreakdownPageCache == null)
                    revenueBreakdownPageCache = new Hashtable();
                revenueBreakdownPageCache[Keys.CumulativeKey] = value;
                pageCachePolicy.Cache(Keys.RevenueBreakdownPageKey, revenueBreakdownPageCache);
            }
        }
    }
}
